﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ViewModel;

namespace IssueTracker.CustomControls
{
    public partial class newIssue : UserControl
    {
        public newIssue()
        {
            InitializeComponent();
        }

        private void newIssue_Load(object sender, EventArgs e)
        {
            cmbPriority.DataSource = Service.NewIssue.GetPriorityOptions();
            cmbPriority.DisplayMember = "Name";
            cmbPriority.ValueMember = "ID";

            cmbStatus.DataSource = Service.NewIssue.GetStatusOptions();
            cmbStatus.DisplayMember = "Name";
            cmbStatus.ValueMember = "ID";

            cmbAssignToOptions.SelectedIndex = 0;
        }

        private void cmbAssignToOptions_SelectedIndexChanged(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            cmbAssignTo.Visible = false;
            switch (cmbAssignToOptions.Text)
            {
                case "All Shops":
                    break;
                case "Area":
                    cmbAssignTo.Visible = true;
                    cmbAssignTo.DataSource = Service.Admin.GetAreas();
                    cmbAssignTo.DisplayMember = "Name";
                    cmbAssignTo.ValueMember = "AreaCode";
                    break;
                case "Group":
                    cmbAssignTo.Visible = true;
                    cmbAssignTo.DataSource = Service.Admin.GetGroups();
                    cmbAssignTo.DisplayMember = "Name";
                    cmbAssignTo.ValueMember = "ID";
                    break;
                case "Shop":
                    cmbAssignTo.Visible = true;
                    cmbAssignTo.DataSource = Service.Admin.GetShops();
                    cmbAssignTo.DisplayMember = "Name";
                    cmbAssignTo.ValueMember = "ID";
                    break;
                default:
                    break;
            }
            Cursor.Current = Cursors.Default;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            lblMessage.Visible = false;
            //check empty field
            if (string.IsNullOrEmpty(txtIssue.Text))
            {
                errorProvider1.SetError(txtIssue, "Issue name can not be empty.");
                return;
            }
            else if (string.IsNullOrEmpty(txtDescription.Text))
            {
                errorProvider1.SetError(txtDescription, "Issue description can not be empty.");
                return;
            }

            List<Shop> assignee = new List<Shop>();
            //members[0].
            switch (cmbAssignToOptions.Text)
            {
                case "All Shops":
                    assignee = Service.Admin.GetShops();
                    break;
                case "Area":
                    assignee = Service.Admin.GetShops(areaCode: cmbAssignTo.SelectedValue.ToString());
                    break;
                case "Group":
                    assignee = Service.Admin.GetShops(groupId: Convert.ToInt32(cmbAssignTo.SelectedValue));
                    break;
                case "Shop":
                    Shop obj = new Shop();
                    obj.ID = Convert.ToInt32(cmbAssignTo.SelectedValue);
                    assignee.Add(obj);
                    break;
                default:
                    break;
            }
            Issue item = new Issue();
            item.Assignee = assignee;
            item.Name = txtIssue.Text;
            item.Priority = (Priority)Convert.ToInt32(cmbPriority.SelectedValue);
            item.Status = (Status)Convert.ToInt32(cmbStatus.SelectedValue);
            item.DateCreated = DateTime.Now;
            item.DateStart = dateTimePickerFrom.Value;
            item.DateEnd = dateTimePickerTo.Value;
            item.Description = txtDescription.Text;
            item.IsRequest = chkIsRequest.Checked;
            int chk = Service.NewIssue.SaveIssueItem(item);
            if (chk > 0)
            {
                lblMessage.Text = "New issue item has been created successfully";
                lblMessage.ForeColor = Color.Green;
                txtIssue.Text = string.Empty;
                txtDescription.Text = string.Empty;
            }
            else
            {
                lblMessage.Text = "Fail !";
                lblMessage.ForeColor = Color.Red;
            }

            lblMessage.Visible = true;
            Cursor.Current = Cursors.Default;
        }

        private void dateTimePickerFrom_ValueChanged(object sender, EventArgs e)
        {
            if (dateTimePickerTo.Value < dateTimePickerFrom.Value)
                dateTimePickerTo.Value = dateTimePickerFrom.Value;
            dateTimePickerTo.MinDate = dateTimePickerFrom.Value;
        }
    }
}
